package com.zxh.zhenxihuan.service.impl;

import com.zxh.zhenxihuan.entity.Classification;
import com.zxh.zhenxihuan.entity.Goods;
import com.zxh.zhenxihuan.entity.Merchants;
import com.zxh.zhenxihuan.entity.MerchantsPic;
import com.zxh.zhenxihuan.mapper.ClassificationMapper;
import com.zxh.zhenxihuan.mapper.GoodsMapper;
import com.zxh.zhenxihuan.mapper.MerchantsMapper;
import com.zxh.zhenxihuan.mapper.MerchantsPicMapper;
import com.zxh.zhenxihuan.service.GoodService;
import com.zxh.zhenxihuan.util.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:geQiang
 * @version:1.0
 * @date:2019/7/26
 * @description:com.zxh.zhenxihuan.service.impl
 */
@Service("goodService")
public class GoodServiceImpl implements GoodService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private MerchantsMapper merchantsMapper;
    @Resource
    private MerchantsPicMapper merchantsPicMapper;
    @Resource
    private ClassificationMapper classificationMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
    public Page<Goods> page(Integer num, Integer count,String productame, Integer importanceOptions) {
        Integer pageNum=1;
        try{
            pageNum=Integer.valueOf(num) ;
        }catch(Exception e){
            e.printStackTrace();
        }
        Integer pageCount=5;
        try{
            pageCount=Integer.valueOf(count);
        }catch(Exception e){
            e.printStackTrace();
        }
        Integer classfi=0;
        try {
            classfi=Integer.valueOf(importanceOptions);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        Page<Goods> page=new Page<>();
        page.init(pageNum,pageCount,getSum(productame,classfi));
        page.setList(getGoods(page.getRowNum(),pageCount,productame,classfi));
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
    public Integer getSum(String productame, Integer importanceOptions) {
        Integer num=goodsMapper.getSum(productame,importanceOptions);
        return num;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
    public List<Goods> getGoods(Integer start, Integer end,String productame, Integer importanceOptions) {
        List<Goods> list = goodsMapper.getGoods(start, end,productame,importanceOptions);
        for (Goods goods : list) {
//            System.out.println(goods);
        }
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
    public Integer del(Integer id) {
        Integer num=goodsMapper.del(id);
        return num;
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
    public Integer add(Goods goods) {
//        分类
        Classification classification=goods.getClassification();
        classificationMapper.addClassification(classification);
//        商家
        Merchants merchants=goods.getMerchants();
        if(merchants.getId()==null){
            merchantsMapper.addMerchants(merchants);
        }
//        商品
        goodsMapper.add(goods);
//        商家图片
        for(MerchantsPic pic:goods.getMerchantsPics()){
            pic.setGoods(goods);
            merchantsPicMapper.addMerchantsPic(pic);
        }
        return 1;
    }

    @Override
    public Goods update(Goods goods) {

        return null;
    }
}
